package com.ltu.timkor6.gamesite.users.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ltu.timkor6.gamesite.shared.CompositeAbstract;

public class SignInComposite extends CompositeAbstract
{
	private VerticalPanel errors;
	private TextBox textUsername;
	private PasswordTextBox textPassword;
	
	public SignInComposite(final SignIn signIn)
	{
		// Title
		final Label title = new Label("Sign In");
		title.setStyleName("panelHeader");
		panel.add(title);
		
		// Errors
		errors = new VerticalPanel();
		errors.setStyleName("errors");
		panel.add(errors);
		
		
		// Sign in table
		FlexTable table = new FlexTable();
		
		Label labelUsername = new Label("Username");
		textUsername = new TextBox();
		textUsername.addKeyPressHandler(new KeyPressHandler()
		{
			public void onKeyPress(KeyPressEvent event)
			{
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
				{
					signIn.checkSubmit();
				}
			}
		});
		
		Label labelPassword = new Label("Password");
		textPassword = new PasswordTextBox();
		textPassword.addKeyPressHandler(new KeyPressHandler()
		{
			public void onKeyPress(KeyPressEvent event)
			{
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
				{
					signIn.checkSubmit();
				}
			}
		});

		
		Button submit = new Button("Sign in");
		submit.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event) {
				signIn.checkSubmit();
			}
		});
		
		table.setWidget(0, 0, labelUsername);
		table.setWidget(0, 1, textUsername);
		table.setWidget(1, 0, labelPassword);
		table.setWidget(1, 1, textPassword);
		table.setWidget(2, 1, submit);
		
		panel.add(table);
		
		textUsername.setFocus(true);
		
		initWidget(panel);
	}
	
	public void addError(String text)
	{
		this.errors.add(new Label(text));
	}
	
	public void clearErrors()
	{
		this.errors.clear();
	}
	
	public String getUsername()
	{
		return this.textUsername.getText().trim();
	}
	
	public String getPassword()
	{
		return this.textPassword.getText().trim();
	}
}
